package com.db.protocol.tags;

import com.db.protocol.exception.IncorrectValueProtocolException;
import com.db.protocol.exception.NotParsingValueProtocolException;
import com.db.protocol.exception.ParseProtocolException;

/**
 * @author nulls.narik
 */
public enum OrderSide {

    BUY(1),
    SELL(2);

    private final int side;

    public int value() {
        return side;
    }

    OrderSide(int side) {
        this.side = side;
    }

    public static Tag tag() {
        return Tag.ORDER_SIDE;
    }

    public static OrderSide parse(String message) throws ParseProtocolException {
        if (message == null) {
            throw new NotParsingValueProtocolException(tag());
        }
        try {
            int value = Integer.valueOf(message);

            for (OrderSide type : values()) {
                if (type.value() == value) {
                    return type;
                }
            }
        } catch (NumberFormatException exception) {
            throw new NotParsingValueProtocolException(tag());
        }
        throw new IncorrectValueProtocolException(tag());
    }

}
